<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use app\api\controller\Index;

class PtController extends BaseController
{	
	protected function _initialize() {
	   $type = get_brower_type();
	   $this->assign('browder_type',$type);
	   $user_info = db('member')->alias("a")->join('member b',"a.pid1=b.id","left")
	   ->where('a.id',session('user_info')['id'])
	   ->field("a.*,b.nick_name as pname,b.real_name as prname")
	   ->find();
	   $this->assign("pname",$user_info['prname']?$user_info['prname']:$user_info['pname']);
		if($user_info['is_city']==1){
			$this->assign("jsname",'美丽导师合伙人');
		}elseif($user_info['is_pt']==1){
			$this->assign("jsname",'美丽事业合伙人');
		}elseif($user_info['is_rz']==1&&$user_info['is_qy']){
			$this->assign("jsname",'形象美学代言人');
		}elseif($user_info['is_rz']==1){
			$this->assign("jsname",'轻创平台创客');
		}else{
			$this->assign("jsname",'注册创客');
		}
		session("user_info",$user_info);
       parent::_initialize();
    }

	//运营中心
    public function index()
    {  	
		$info_user = session('user_info');
//		if(!$info_user['logo']){
//			parent::up_user(session("openid"));
//		}

		$myfriends=db("member")->where("pid1",$info_user['id'])->count();
		$this->assign("counts",$myfriends);
		
		$ptcount=db("member")->where("old_pt",$info_user['id'])->count();
		$this->assign("ptcount",$ptcount);
    	$this->assign('info_user',$info_user);
		
		$tjname = db("member")->where("id",$info_user['pid1'])->find();
		if($tjname){
			$this->assign("tjname",$tjname['mobile']);
		}

		//获取当前用户订单
		$order_list = db('good_order')->where('user_id',session('user_id'))->select();
		
		$dfk_num = 0;
		$dfh_num = 0;
		$dsh_num = 0;
		$dpj_num = 0;
		foreach($order_list as $key=>$value){
			if($value['status'] == 1){
				$dfk_num = $dfk_num+1;
			}
			
			if($value['status'] == 2){
				$dfh_num = $dfh_num+1;
			}
			
			if($value['status'] == 3){
				$dsh_num = $dsh_num+1;
			}
			
			/*if($value['status'] == 4){
				$dpj_num = $dpj_num+1;
			}*/
		}
		$order_num['dfk_num'] = $dfk_num;
		$order_num['dfh_num'] = $dfh_num;
		$order_num['dsh_num'] = $dsh_num;
		//$order_num['dpj_num'] = $dpj_num;
		
		$this->assign("order_num",$order_num);
		$this->assign("url",'index');
       	return  $this->fetch();
    }
	
	//会员管理
	public function member(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$member = db('member')->where("city_id",$info_user['id'])->order('add_time desc')->limit($begin,10)->select();
			
			if($member){
				return json(['code'=>200,"list"=>$member]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	
	//合伙人列表
    public function pt_list()
    {    	
		if($_POST){
			$info_user = session('user_info');
			$tjcount = db('member')->field('pt_id,count(id) as count')->group('pt_id')->buildSql();
			
			$page = 1;
			if(input("page")){
				$page = input("page");
			}
			$begin = ($page-1)*10;
			$ptlist = db('member')->alias('a')
			->join($tjcount." b","a.id=b.pt_id",'LEFT')
			->field('a.*,b.count')
			->where('a.is_pt',1)
			->where('a.city_id',$info_user['id'])->order('a.add_time desc')->limit($begin,10)->select();
			if($ptlist){
				return json(['code'=>200,"list"=>$ptlist]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
    }
	
	//合伙人申请列表
    public function ptsh_list()
    {    	
		if($_POST){
			$page = 1;
			if(input("page")){
				$page = input("page");
			}
			$begin = ($page-1)*10;
			$ptlist = db('member')->where('is_pt',3)->order('add_time desc')->limit($begin,10)->select();
			if($ptlist){
				return json(['code'=>200,"list"=>$ptlist]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
    }
	
	//红包记录
	public function hb_list(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$where['user_id'] = $info_user['id'];
			$where['type'] = '1';
			$logs=db('hb_list')->where($where)->order('add_time desc')->limit($begin,10)->select();
			if($logs){
				return json(['code'=>200,"list"=>$logs]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	
	//佣金记录
	public function yj_list(){
		if($_POST){
			$info_user = session('user_info');
			$page=1;
			$where['user_id']=$info_user['id'];
			if(input("page")){
				$page=input("page");
			}
			$begin=($page-1)*10;
			$where['user_id'] = $info_user['id'];
			$where['type'] = '4';
			$logs=db('hb_list')->where($where)->order('add_time desc')->limit($begin,10)->select();
			if($logs){
				return json(['code'=>200,"list"=>$logs]);
			}else{
				return json(['list'=>null]);
			}
		}
		return $this->fetch();
	}
	
	//运营中心
	public function yunying(){
		$userinfo = db('member')->where('id',$info_user['id'])->find();
		$this->assign('is_city',$userinfo['is_city']);
		return  $this->fetch();
	}
	
	//推荐合伙人佣金
	function tjptyj(){
		$user_info = session("user_info");
		$w['a.user_id'] = $user_info['id'];
		$w1['a.user_id'] = $user_info['id'];
		$w2['a.user_id'] = $user_info['id'];
		$config = get_config('ticket_config');
		if($user_info['is_city']==1){
			$w['a.type'] = ["in",[9,19]];
		}else{
			$w['a.type'] = 9;
		}
		
		if($_POST){
			$list = db('gdyj_record')->alias('a')
			->join('member b','a.mid = b.id','LEFT')
			->field('a.id,a.type,a.message,a.balance,a.add_time,b.nick_name,b.nick_name as n,b.id as b,mid')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$w1['a.type'] =9;
		$field = "count(a.id) as c,sum(a.balance) s";
		$log = db('gdyj_record')->alias('a')->where($w1)->field($field)->find();
		
		$w2['a.type'] =19;
		$field = "count(a.id) as c,sum(a.balance) s";
		$log1 = db('gdyj_record')->alias('a')->where($w2)->field($field)->find();
		if(!$log){
			$log['c'] = 0;
			$log['s'] = 0;
		}
		if(!$log1){
			$log1['c'] = 0;
			$log1['s'] = 0;
		}
		$this->assign("config",$config);
		$this->assign('log',$log);
		$this->assign('log1',$log1);
		return $this->fetch('');
	}
		//推荐运营商佣金
	function tjctyj(){
		$user_info = session("user_info");
		$w['a.user_id'] = $user_info['id'];
		$config = get_config('ticket_config');
		$w['a.type'] = 20;
		if($_POST){
			$list = db('gdyj_record')->alias('a')
			->join('member b','a.mid = b.id','LEFT')
			->field('a.id,a.message,a.balance,a.add_time,b.nick_name,b.nick_name as n')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field = "count(a.id) as c,sum(a.balance) s";
		$log = db('gdyj_record')->alias('a')->where($w)->field($field)->find();
		if(!$log){
			$log['c'] = 0;
			$log['s'] = 0;
		}
		$this->assign("config",$config);
		$this->assign('log',$log);
		return $this->fetch('');
	}
	
	//奖励金
	function jlj(){
		$user_info=session("user_info");
		$w['user_id']=$user_info['id'];
		$config =get_config('ticket_config');
		$w['type']=5;
		if($_POST){
			$list = db('hb_list')->field('id,message,balance,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field="count(id) as c,sum(balance) s";
		$log=db('hb_list')->where($w)->field($field)->find();
		if(!$log){
			$log['c']=0;
			$log['s']=0;
		}
		$this->assign("config",$config);
		$this->assign('log',$log);
		return $this->fetch('');
	}

	//合伙人间推佣金
	function jtyj(){
		$user_info = session("user_info");
		$w['user_id'] = $user_info['id'];
		$config = get_config('ticket_config');
		$w['type'] = 7;
		if($_POST){
			$list = db('gdyj_record')->field('id,message,balance,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field = "count(id) as c,sum(balance) s";
		$log = db('gdyj_record')->where($w)->field($field)->find();
		if(!$log){
			$log['c'] = 0;
			$log['s'] = 0;
		}
		$this->assign("config",$config);
		$this->assign('log',$log);
		return $this->fetch('');
	}

	//合伙人直推佣金
	function ztyj(){
		$user_info = session("user_info");
		$w['user_id'] = $user_info['id'];
		$config = get_config('ticket_config');
		$w['type'] = 6;
		if($_POST){
			$list = db('gdyj_record')->field('id,message,balance,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field = "count(id) as c,sum(balance) s";
		$log = db('gdyj_record')->where($w)->field($field)->find();
		if(!$log){
			$log['c'] = 0;
			$log['s'] = 0;
		}
		$this->assign("config",$config);
		$this->assign('log',$log);
		return $this->fetch('');
	}

	//合伙人佣金
	function ptyj(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		$w['a.pt_id'] = $user_id;
		if($_POST){
			$list = db('paylog')->alias('a')
			->join('member b','a.user_id = b.id','LEFT')
			->field('a.user_id,a.state,a.balance,a.pt_yj,a.add_time,b.nick_name,b.mobile,b.real_name')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$field = "count(id) as c,sum(balance) s";
		
		$where['user_id'] = $user_id;
		$where['type']=["in",[8,12,16]];
		$zyj=db('gdyj_record')->where($where)->sum('balance');
		$this->assign('zyj', $zyj);
		
		$where1['user_id'] = $user_id;
		$where1['type'] = 6;//直推		
		$ztyj=db('gdyj_record')->where($where1)->sum('balance');
		$this->assign('ztyj', $ztyj);

		$where2['user_id'] = $user_id;
		$where2['type'] = 7;//间推		
		$jtyj=db('gdyj_record')->where($where2)->sum('balance');
		$this->assign('jtyj', $jtyj);

		$where3['pt_id'] = $user_id;	
		$where3['status']=4;	
		$gyj = db('good_order')->where($where3)->sum('pt_yj');
		$this->assign('gyj', $gyj);

		return $this->fetch('');
	}
	//运营中心奖励
	function ctyj(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		$where['city_id'] = $user_id;
		$where['type']=["in",[5,8,9,12,13,16,17,19]];
		if($_POST){
			$list = db('gdyj_record')->where($where)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$zyj=db('gdyj_record')->where($where)->sum('balance');
		$this->assign('zyj', $zyj);
		
		$where1['city_id'] = $user_id;
		$where1['type']=["in",[8,9,12,16]];//合伙人	
		$ptyj=db('gdyj_record')->where($where1)->sum('balance');
		$this->assign('ptyj', $ptyj);

		$where2['city_id'] = $user_id;
		$where2['type']=["in",[5,13,17,19]];//运营中心		
		$ctyj=db('gdyj_record')->where($where2)->sum('balance');
		$this->assign('ctyj', $ctyj);

		return $this->fetch('');
	}
	//合伙人团队营业额
	function pttdyye(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		$w['a.pt_id'] = $user_id;
		if(input('name')){
			$w['b.id|b.mobile|b.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$w['state']=2;
			$list = db('paylog')->alias('a')
			->join('member b','a.user_id = b.id','LEFT')
			->field('a.user_id,a.state,a.balance,a.pt_yj,a.add_time,b.nick_name')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		$where['pt_id'] = $user_id;
		$ptyye=db('member')->where($where)->sum('total_xf');
		$this->assign('ptyye', $ptyye);
		
		$where1['pt_id'] = $user_id;
		$where1['pid1'] = $user_id;//直推		
		$ztyj=db('member')->where($where1)->sum('total_xf');
		$this->assign('ztyj', $ztyj);

		$where2['pt_id'] = $user_id;
		$where2['pid2'] = $user_id;
		$jtyj=db('member')->where($where2)->sum('total_xf');
		$this->assign('jtyj', $jtyj);

		return $this->fetch('');
	}

	//团队直接推荐合伙人
	function zj_tjpt(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		
		//团队人数
		$tdcount = db('member')->where('old_pt',$user_id)->count();
		$this->assign('tdcount',$tdcount);

		$w['old_pt'] = $user_id;
		$w['is_pt'] = 1;		
		//合伙人人数
		$ptcount = db('member')->where($w)->count();
		$this->assign('ptcount',$ptcount);
		

		if(input('name')){
			$w['id|mobile|nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('member')->field('id,nick_name,mobile,total_yj,add_time')->where($w)->order("add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}		
		
		return $this->fetch();
	}
	
	//推荐合伙人团队营业额
	function tjpttdyye(){
		$user_info = session("user_info");
		$user_id = $user_info['id'];
		//推荐的合伙人团队
		$pt_list = db('member')->field('id')->where('old_pt',$user_id)->buildSql();
		
		//查询列表
		$w['a.pt_id'] = $user_id;
		if(input('name')){
			$w['a.id|a.mobile|a.nick_name'] = ['like','%'.input('name').'%'];
		}
		if($_POST){
			$list = db('member')->alias('a')
			->join($pt_list.' b','a.pt_id = b.id','LEFT')
			->field('a.id,a.nick_name,a.mobile,a.total_xf,update_time')->where($w)->order("a.add_time desc")->page(input("page"),10)->select();
			return json(['list'=>$list]);
		}
		
		//加入合伙人费用  合伙人推荐合伙人的业绩
		$config = get_config('ticket_config');
		$jrpt = $config['p_balance'];
		$tjptnum = db('member')->where('old_pt',$user_id)->count();
		$jrpt_fy = $jrpt*$tjptnum;
		
		//推荐的合伙人自己消费的业绩
		$ptlist_sum = db('member')->where('old_pt',$user_id)->sum('total_xf');
				
		//推荐的合伙人推荐的会员业绩
		$ptmember_sum = db('member')->alias('a')
			->join($pt_list.' b','a.pt_id = b.id','LEFT')
			->sum('a.total_xf');

		$tjpt_sum = (float)$jrpt_fy + (int)$ptlist_sum + (int)$ptmember_sum;
		$this->assign('tjpt_sum', $tjpt_sum);

		return $this->fetch('');
	}
}
